package com.xusheng.cli.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.xusheng.cli.common.annotation.OperationLogAnno;
import com.xusheng.cli.common.base.MyPage;
import com.xusheng.cli.common.base.Result;
import com.xusheng.cli.common.enums.OperationType;
import com.xusheng.cli.common.validation.Save;
import com.xusheng.cli.common.validation.Update;
import com.xusheng.cli.entity.permissionModule.Permission;
import com.xusheng.cli.service.PermissionService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.constraints.Size;
import java.io.Serializable;
import java.util.List;

/**
 * 总后台 权限表(Permission)表控制层
 *
 * @author jy
 * @since 2022-01-17 15:47:07
 */
@Validated
@RestController
@RequestMapping("/api/v1/permission")
public class PermissionController {

	/**
	 * 服务对象
	 */
	@Resource
	private PermissionService permissionService;

	/**
	 * 获取整个权限树
	 *
	 * @author jy
	 * @since 2022/1/18 10:46
	 */
	@GetMapping("permissionTree")
	public Result<List<Permission>> getPermissionTree() {
		return Result.success(permissionService.getPermissionTree());
	}

	@DeleteMapping("menuOrPermission")
	@OperationLogAnno(module = "权限表模块", type = OperationType.UPDATE, desc = "停启用权限")
	Result<Object> delMenuOrPermission(Long id) {
		Permission permission = new Permission();
		LambdaUpdateWrapper<Permission> updateWrapper = Wrappers.lambdaUpdate();
		updateWrapper.eq(Permission::getInitial, false).setSql("enable = not enable").eq(Permission::getId, id);
		return Result.isSuccess(permissionService.update(permission, updateWrapper));
	}

	/**
	 * 查询所有数据
	 *
	 * @param permission 查询实体
	 * @return 所有数据
	 * @author jy
	 * @since 2022-01-17 15:47:07
	 */
	@GetMapping("queryAll")
	public Result<List<Permission>> queryAll(Permission permission) {
		permission.setEnable(true);
		return Result.success(permissionService.list(new QueryWrapper<>(permission)));
	}

	/**
	 * 分页查询所有数据
	 *
	 * @param page       分页对象
	 * @param permission 查询实体
	 * @return 所有数据
	 * @author jy
	 * @since 2022-01-17 15:47:07
	 */
	@GetMapping("queryByPage")
	public Result<MyPage<Permission>> queryByPage(MyPage<Permission> page, Permission permission) {
		return Result.success(permissionService.page(page, new QueryWrapper<>(permission)));
	}

	/**
	 * 通过主键查询单条数据
	 *
	 * @param id 主键
	 * @return 单条数据
	 * @author jy
	 * @since 2022-01-17 15:47:07
	 */
	@GetMapping("queryOne/{id}")
	public Result<Permission> selectOne(@PathVariable Serializable id) {
		return Result.success(permissionService.getById(id));
	}

	/**
	 * 新增数据
	 *
	 * @param permission 实体对象
	 * @return 新增结果
	 * @author jy
	 * @since 2022-01-17 15:47:07
	 */
	@PostMapping("insert")
	@OperationLogAnno(module = "权限模块", type = OperationType.CREATE, desc = "新增数据")
	public Result<Object> insert(@Validated(Save.class) @RequestBody Permission permission) {
		return Result.isSuccess(permissionService.insert(permission));
	}

	/**
	 * 修改数据
	 *
	 * @param permission 实体对象
	 * @return 修改结果
	 * @author jy
	 * @since 2022-01-17 15:47:07
	 */
	@PutMapping("update")
	@OperationLogAnno(module = "权限模块", type = OperationType.UPDATE, desc = "修改数据")
	public Result<Object> update(@Validated(Update.class) @RequestBody Permission permission) {
		return Result.isSuccess(permissionService.update(permission));
	}

	/**
	 * 删除单条数据
	 *
	 * @param id 主键
	 * @return 删除结果
	 * @author jy
	 * @since 2022-01-17 15:47:07
	 */
	@DeleteMapping("delete/{id}")
	@OperationLogAnno(module = "权限模块", type = OperationType.DELETE, desc = "删除单条数据")
	public Result<Object> delete(@PathVariable Long id) {
		Permission permission = new Permission();
		LambdaUpdateWrapper<Permission> updateWrapper = Wrappers.lambdaUpdate();
		updateWrapper.eq(Permission::getId, id).eq(Permission::getInitial, false);
		return Result.isSuccess(permissionService.deleteWithFill(permission, updateWrapper));
	}

	/**
	 * 批量删除数据
	 *
	 * @param idList 主键结合
	 * @return 删除结果
	 * @author jy
	 * @since 2022-01-17 15:47:07
	 */
	@DeleteMapping("batchDelete")
	@OperationLogAnno(module = "权限模块", type = OperationType.DELETE, desc = "批量删除数据")
	public Result<Object> batchDelete(@Validated @Size(min = 1, message = "缺少删除所需的id") @RequestParam("idList") List<Long> idList) {
		Permission permission = new Permission();
		permission.setInitial(false);
		return Result.isSuccess(permissionService.batchDelete(idList, permission));
	}

}

